package com.wanyu.video.nio.selector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {

    public static void main(String[] args) throws IOException {
        // 得到一个网络通道
        SocketChannel socketChannel = SocketChannel.open();

        // 设置非阻塞
        socketChannel.configureBlocking(false);

        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10086);

        if(!socketChannel.connect(address)){
            while(!socketChannel.finishConnect()){
                System.out.println("正在建立连接");
            }
        }
        String str = "Hello World";

        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(buffer);
        // 防止发送完就直接关闭连接，导致服务端报错
        System.in.read();

    }
}
